/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Dec 28, 2012 8:41:56 PM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com.kanbox.linux.fs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kanbox.linux.GlobalConf;
import com.kanbox.linux.operation.Information;

public class MountPointManager implements Serializable {
	private static Logger logger = LoggerFactory
			.getLogger(MountPointManager.class);

	private static final long serialVersionUID = -2431107572001329752L;
	private String mountPath;
	private String dataPath;

	private static final MountPointManager THE_MOUNTPOINT_MANAGER = new MountPointManager();

	private MountPointManager() {
		try {
			read();
		} catch (ClassNotFoundException | IOException e) {
			try {
				mountPath = System.getProperty("user.home")
						+ "/kanboxmountpoint";

				String email = new Information().getEmail();
				dataPath = GlobalConf.METAPATH + "/" + email;

				new File(mountPath).mkdirs();
				new File(dataPath).mkdirs();

				save();
			} catch (IOException e1) {
				logger.error("Error when saving mount point file", e);
			}
		}
	}

	private void save() throws FileNotFoundException, IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				new File(GlobalConf.METAPATH + "/.mountpoint")));
		oos.writeObject(this);
		oos.close();
	}

	private void read() throws ClassNotFoundException, IOException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				new File(GlobalConf.METAPATH + "/.mountpoint")));
		MountPointManager mpm = (MountPointManager) ois.readObject();
		mountPath = mpm.mountPath;
		dataPath = mpm.dataPath;
		ois.close();
	}

	public String getMountPath() {
		return mountPath;
	}

	public void setMountPath(String mountPath) {
		this.mountPath = mountPath;
		try {
			save();
		} catch (IOException e) {
			logger.error("Error when saving mount point file", e);
		}
	}

	public String getDataCachePath() {
		return dataPath;
	}

	@Deprecated
	public void setDataCachePath(String dataPath) {
		this.dataPath = dataPath;
		try {
			save();
		} catch (IOException e) {
			logger.error("Error when saving mount point file", e);
		}
	}

	public static void setMountPoint(String mountPoint) {
		THE_MOUNTPOINT_MANAGER.setMountPath(mountPoint);
	}

	public static String getMountPoint() {
		return THE_MOUNTPOINT_MANAGER.getMountPath();
	}

	@Deprecated
	public static void setDataPath(String dataPath) {
		THE_MOUNTPOINT_MANAGER.setDataCachePath(dataPath);
	}

	public static String getDataPath() {
		return THE_MOUNTPOINT_MANAGER.getDataCachePath();
	}

	public static String mountToDataPath(String path) {
		return path.replace(getMountPoint(), getDataPath());
	}

	public static String dataToMountPath(String path) {
		return path.replace(getDataPath(), getMountPoint());
	}

	public static String remoteToLocalDataPath(String path) {
		return getDataPath() + path;
	}

	public static String localToRemoteDataPath(String path) {
		path = path.replaceFirst(getDataPath(), "/");
		while (path.startsWith("//")) {
			path = path.replaceFirst("//", "/");
		}
		return path;
	}

	public static void main(String[] args) {
		System.out
				.println(dataToMountPath("/home/yezi/.kanbox/okye328@qq.com/hello"));

		System.out
				.println(mountToDataPath("/home/yezi/kanboxmountpoint/world"));
	}

}
